#include "LB_searchbook.h"
#include "ui_LB_searchbook.h"


#include <QMessageBox>
#include <QList>
#include <QDebug>
#include <QTextStream>
#include <string.h>
#include <QPalette>



//通过
LB_searchbook::LB_searchbook(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LB_searchbook)
{
    ui->setupUi(this);

    //QPixmap pixmap = QPixmap("D://Qt//workspace//bookManager//pictures//xiyang.jpg").scaled(this->size());
    QPixmap pixmap = QPixmap("D://CsSoftware//Qt//__MyFiles//workspace//bookManager//pictures//mountain.jpg").scaled(this->size());
    QPalette palette(this->palette());
    palette.setBrush(QPalette::Background,pixmap);
    this->setPalette(palette);


    this->model = new QStandardItemModel;//new出来一个表格模型
    //内容归model管，外观归tableview管

    this->model->setHorizontalHeaderItem(0,new QStandardItem("ISBN号"));//QStandardItem是单元格
    this->model->setHorizontalHeaderItem(1,new QStandardItem("书名"));
    this->model->setHorizontalHeaderItem(2,new QStandardItem("作者"));
    this->model->setHorizontalHeaderItem(3,new QStandardItem("出版社"));
    this->model->setHorizontalHeaderItem(4,new QStandardItem("进书日期"));
    this->model->setHorizontalHeaderItem(5,new QStandardItem("库存量"));
    this->model->setHorizontalHeaderItem(6,new QStandardItem("批发价"));
    this->model->setHorizontalHeaderItem(7,new QStandardItem("零售价"));

    this->ui->tableView->setModel(model);//让view显示视图

    //设置单元格宽度
    this->ui->tableView->setColumnWidth(0,100);
    this->ui->tableView->setColumnWidth(1,200);
    this->ui->tableView->setColumnWidth(2,150);
    this->ui->tableView->setColumnWidth(3,300);
    this->ui->tableView->setColumnWidth(4,200);
    this->ui->tableView->setColumnWidth(5,100);
    this->ui->tableView->setColumnWidth(6,100);
    this->ui->tableView->setColumnWidth(7,100);

}

//pass
LB_searchbook::~LB_searchbook()
{
    delete ui;
}

//pass
void LB_searchbook::on_pushButton_clicked()//用户点击查询后调用此函数
{

    this->model->clear();

    this->model->setHorizontalHeaderItem(0,new QStandardItem("ISBN号"));//QStandardItem是单元格
    this->model->setHorizontalHeaderItem(1,new QStandardItem("书名"));
    this->model->setHorizontalHeaderItem(2,new QStandardItem("作者"));
    this->model->setHorizontalHeaderItem(3,new QStandardItem("出版社"));
    this->model->setHorizontalHeaderItem(4,new QStandardItem("进书日期"));
    this->model->setHorizontalHeaderItem(5,new QStandardItem("库存量"));
    this->model->setHorizontalHeaderItem(6,new QStandardItem("批发价"));
    this->model->setHorizontalHeaderItem(7,new QStandardItem("零售价"));

    this->ui->tableView->setModel(model);//让view显示视图

    //设置单元格宽度
    this->ui->tableView->setColumnWidth(0,100);
    this->ui->tableView->setColumnWidth(1,200);
    this->ui->tableView->setColumnWidth(2,150);
    this->ui->tableView->setColumnWidth(3,300);
    this->ui->tableView->setColumnWidth(4,200);
    this->ui->tableView->setColumnWidth(5,100);
    this->ui->tableView->setColumnWidth(6,100);
    this->ui->tableView->setColumnWidth(7,100);


    QString cnt = this->ui->le_cnt->text();//获取用户输入信息
    doQuery(cnt);
}

//pass
void LB_searchbook::doQuery(QString cnt)
{

    int row = 0;
    for(int i = 0; i < 100 ;i++){
        if((dataArray[i].bookTitle.contains(cnt,Qt::CaseInsensitive)||dataArray[i].isbn.contains(cnt,Qt::CaseInsensitive)||dataArray[i].publisher.contains(cnt,Qt::CaseInsensitive))&&dataArray[i].existOrNot){//是否被标记为存在

            this->model->setItem(row,0,new QStandardItem(dataArray[i].isbn));
            this->model->setItem(row,1,new QStandardItem(dataArray[i].bookTitle));
            this->model->setItem(row,2,new QStandardItem(dataArray[i].author));
            this->model->setItem(row,3,new QStandardItem(dataArray[i].publisher));
            this->model->setItem(row,4,new QStandardItem(dataArray[i].dataAdded));
            this->model->setItem(row,5,new QStandardItem(dataArray[i].qtyOnHand));
            this->model->setItem(row,6,new QStandardItem(dataArray[i].wholesale));
            this->model->setItem(row,7,new QStandardItem(dataArray[i].retail));
          row++;
        }
    }
}


void LB_searchbook::on_pushButton_2_clicked()
{
    this->close();
}
